#include "colisioneditor.h"
#include "ui_colisioneditor.h"
#include <QInputDialog>
#include <QMessageBox>
#include <QDebug>

ColisionEditor::ColisionEditor(QWidget *parent) : QWidget(parent),
    ui(new Ui::ColisionEditor)
{
    qDebug() << "collisionEditor";
    ui->setupUi(this);

    connect(ui->addGroup, SIGNAL(clicked()), this, SLOT(AddGroup()));
    connect(ui->delGroup, SIGNAL(clicked()), this, SLOT(DelGroup()));
    connect(ui->listGroups, SIGNAL(currentTextChanged(QString)), this, SLOT(GroupChanged(QString)));
    connect(ui->listColideWith, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(ColideWithChanged(QListWidgetItem*)));
    qDebug() << "end collisionEditor";
}

ColisionEditor::~ColisionEditor()
{
    delete ui;
}

void ColisionEditor::AddGroup()
{
    QString name = QInputDialog::getText(this, "Nouveau groupe", "Nom du groupe");

    if (name != "")
    {
	ColisionGroup newGroup;

	newGroup.name = name;
	_groups << newGroup;
	ui->listGroups->addItem(name);
	if (ui->listGroups->count() == 8)
	    ui->addGroup->setEnabled(false);
	GroupAdded(name);
    }
}

void ColisionEditor::DelGroup()
{
    QListWidgetItem* item = ui->listGroups->currentItem();

    if (item)
    {
	QString                        name = item->text();
	QList<ColisionGroup>::iterator it   = _groups.begin();
	QList<ColisionGroup>::iterator end  = _groups.end();

	while (it != end)
	{
	    (*it).colideWith.removeAll(name);
	    if ((*it).name == name)
		_groups.erase(it);
	    ++it;
	}
	ui->addGroup->setEnabled(true);
	delete item;
	GroupDeleted(name);
    }
}

void ColisionEditor::GroupChanged(QString name)
{
    QList<ColisionGroup>::iterator it   = _groups.begin();
    QList<ColisionGroup>::iterator end  = _groups.end();

    while (it != end)
    {
	if ((*it).name == name)
	{
	    ui->listColideWith->clear();
	    QList<ColisionGroup>::iterator colIt  = _groups.begin();
	    QList<ColisionGroup>::iterator colEnd = _groups.end();

	    for (; colIt != colEnd ; ++colIt)
	    {
		QListWidgetItem* item;

		ui->listColideWith->addItem((*colIt).name);
		item = ui->listColideWith->item(ui->listColideWith->count() - 1);
		foreach (QString colisionWith, (*it).colideWith)
		{
		    if (colisionWith == (*colIt).name)
		    {
			(*colIt).colideWith.removeAll(name);
			(*colIt).colideWith << name;
			item->setSelected(true);
		    }
		}
	    }
	}
	++it;
    }
}

void ColisionEditor::ColideWithChanged(QListWidgetItem* item)
{
    QList<ColisionGroup>::iterator it   = _groups.begin();
    QList<ColisionGroup>::iterator end  = _groups.end();

    while (it != end)
    {
	if ((*it).name == ui->listGroups->currentItem()->text())
	{
	    if (item->isSelected())
		(*it).colideWith << item->text();
	    else
		(*it).colideWith.removeAll(item->text());
	}
	++it;
    }
}

QString ColisionEditor::GenerateMask(ColisionGroup group)
{
    QString toRet;
    char    i = 0;

    for (; i < ui->listGroups->count() ; ++i)
    {
	QString name        = ui->listGroups->item(i)->text();
	bool    doesCollide = false;

	foreach (QString collideWith, group.colideWith)
	{
	    if (collideWith == name)
	    {
		doesCollide = true;
		break ;
	    }
	}
	toRet += (doesCollide ? '1' : '0');
    }
    for (; i < 8 ; ++i)
	toRet += '0';
    return (toRet);
}
#include <iostream>
void ColisionEditor::Load(Data data)
{
    Data::iterator it  = data.begin();
    Data::iterator end = data.end();

    for (; it != end ; ++it)
    {
	ColisionGroup newGroup;

    newGroup.name = (*it).Key().c_str();
	ui->listGroups->addItem(newGroup.name);
	GroupAdded(newGroup.name);
    }
}

void ColisionEditor::Save(YamlStream& stream)
{
    if (_groups.count())
    {
	stream.newBlock("collisionGroups");
	foreach (ColisionGroup group, _groups)
	{
	    QString                     collisionMask = GenerateMask(group);
	    QPair<QString, std::string> pair("mask", collisionMask.toStdString());

	    stream.newBlock(group.name);
	    stream << pair;
	    stream.endBlock();
	}
	stream.endBlock();
    }
}
